package com.demo.androidgame.samgukji_hit;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class SenarioScreen extends GLScreen {

	Camera2D guiCam;
	SpriteBatcher batcher;
	Rectangle next_bt;
	Rectangle skip_bt;
	Vector2 touchPoint;
	public int senario_no;
	
	public SenarioScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 800, 480);
		batcher = new SpriteBatcher(glGraphics, 100);
		next_bt = new Rectangle(680, 20, 100, 40);
		skip_bt = new Rectangle(680, 420, 100, 40);
		touchPoint = new Vector2();
	}

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);                        
            if(event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);
                
                if (OverlapTester.pointInRectangle(skip_bt, touchPoint)) {
                	game.setScreen(new SelectScreen(game));
                	return;
                }
                if (OverlapTester.pointInRectangle(next_bt, touchPoint)) {
                	if (senario_no == 0) {
                		senario_no = 1;
                	} else if (senario_no == 1) {
                		senario_no = 2;
                	} else if (senario_no == 2) {
                		senario_no = 3;
                		game.setScreen(new SelectScreen(game));
                		return;
                	}
                }
            }
        }
	}

	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
		batcher.beginBatch(Assets.skip_button);
		batcher.drawSprite(730, 440, 100, 40, Assets.skip_buttonRegion);
		batcher.endBatch();
		
		batcher.beginBatch(Assets.next_button);
		batcher.drawSprite(730, 40, 100, 40, Assets.next_buttonRegion);
		batcher.endBatch();
		
		gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 
        
        if(senario_no == 0) {
        	batcher.beginBatch(Assets.text_box);
        	batcher.drawSprite(350, 70, 600, 100, Assets.text_box1);
        	batcher.endBatch();
        
        	batcher.beginBatch(Assets.sam_story);
        	batcher.drawSprite(350, 295, 600, 350, Assets.sam_story1);
        	batcher.endBatch();
        } else if (senario_no == 1) {
        	batcher.beginBatch(Assets.text_box);
        	batcher.drawSprite(350, 70, 600, 100, Assets.text_box2);
        	batcher.endBatch();
        
        	batcher.beginBatch(Assets.sam_story);
        	batcher.drawSprite(350, 295, 600, 350, Assets.sam_story2);
        	batcher.endBatch();
        } else if (senario_no == 2) {
        	batcher.beginBatch(Assets.text_box);
        	batcher.drawSprite(350, 70, 600, 100, Assets.text_box3);
        	batcher.endBatch();
        	
        	batcher.beginBatch(Assets.sam_story);
        	batcher.drawSprite(350, 295, 600, 350, Assets.sam_story3);
        	batcher.endBatch();
        }
        gl.glDisable(GL10.GL_BLEND);
		gl.glDisable(GL10.GL_TEXTURE_2D);
	}

	@Override
	public void pause() {
		
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
